package com.meowcook.service.impl;

import com.meowcook.exception.AppException;
import com.meowcook.common.AppResult;
import com.meowcook.common.ResultCode;
import com.meowcook.dao.*;
import com.meowcook.mapper.LikePojoMapper;
import com.meowcook.pojo.dto.LikeCommentDTO;
import com.meowcook.pojo.dto.LikeRecipeDTO;
import com.meowcook.pojo.dto.LikeReplyDTO;
import com.meowcook.pojo.po.*;
import com.meowcook.pojo.vo.LikeCommentVO;
import com.meowcook.pojo.vo.LikeRecipeVO;
import com.meowcook.pojo.vo.LikeReplyVO;
import com.meowcook.service.ILikeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2025-05-15
 * Time: 15:41
 */
@Service
public class LikeServiceImpl implements ILikeService {
    @Resource
    private LikePojoMapper likePojoMapper;
    @Resource
    private LikeRecipeMapper likeRecipeMapper;
    @Resource
    private RecipeMapper recipeMapper;
    @Resource
    private LikeCommentMapper likeCommentMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private LikeReplyMapper likeReplyMapper;
    @Resource
    private ReplyMapper replyMapper;
    @Override
    public void thumbsUpRecipe(LikeRecipeDTO likeRecipeDTO) {
        // 数据转换
        LikeRecipe likeRecipe = likePojoMapper.toPO(likeRecipeDTO);
        // id 为空则新增点赞关联 不为空则更新点赞状态
        if (likeRecipe.getId() == null) {
            likeRecipe.setCreateTime(new Date());
            likeRecipe.setUpdateTime(new Date());
            int row = likeRecipeMapper.insertSelective(likeRecipe);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        } else {
            likeRecipe.setUserId(null);
            likeRecipe.setRecipeId(null);
            int row = likeRecipeMapper.updateByPrimaryKeySelective(likeRecipe);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
        // 更新食谱点赞数
        Recipe recipe = recipeMapper.selectByPrimaryKey(likeRecipeDTO.getRecipeId());
        Recipe updateRecipe = new Recipe();
        updateRecipe.setId(likeRecipeDTO.getRecipeId());
        if (likeRecipeDTO.getIsLiked() == 1) {
            updateRecipe.setLikeCount(recipe.getLikeCount()+1);
            int row = recipeMapper.updateByPrimaryKeySelective(updateRecipe);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        } else {
            updateRecipe.setLikeCount(recipe.getLikeCount()-1);
            int row = recipeMapper.updateByPrimaryKeySelective(updateRecipe);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
    }

    @Override
    public LikeRecipeVO selectLikeRecipeRecord(Long userId, Long recipeId) {
        LikeRecipe likeRecipe = likeRecipeMapper.selectByUserIdAndRecipeId(userId, recipeId);
        if (likeRecipe == null) return null;
        return likePojoMapper.toVO(likeRecipe);
    }

    @Override
    public void thumbsUpComment(LikeCommentDTO likeCommentDTO) {
        // 数据转换
        LikeComment likeComment = likePojoMapper.toPO(likeCommentDTO);
        // id 为空则新增点赞关联 不为空则更新点赞状态
        if (likeComment.getId() == null) {
            likeComment.setCreateTime(new Date());
            likeComment.setUpdateTime(new Date());
            int row = likeCommentMapper.insertSelective(likeComment);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        } else {
            likeComment.setUserId(null);
            likeComment.setCommentId(null);
            int row = likeCommentMapper.updateByPrimaryKeySelective(likeComment);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
        // 更新评论点赞数
        Comment comment = commentMapper.selectByPrimaryKey(likeCommentDTO.getCommentId());
        Comment updateComment = new Comment();
        updateComment.setId(likeCommentDTO.getCommentId());
        if (likeCommentDTO.getIsLiked() == 1) {
            updateComment.setLikeCount(comment.getLikeCount()+1);
            int row = commentMapper.updateByPrimaryKeySelective(updateComment);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        } else {
            updateComment.setLikeCount(comment.getLikeCount()-1);
            int row = commentMapper.updateByPrimaryKeySelective(updateComment);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
    }

    @Override
    public LikeCommentVO selectLikeCommentRecord(Long userId, Long commentId) {
        LikeComment likeComment = likeCommentMapper.selectByUserIdAndCommentId(userId, commentId);
        if (likeComment == null) return null;
        return likePojoMapper.toVO(likeComment);
    }

    @Override
    public LikeReplyVO selectLikeReplyRecord(Long userId, Long replyId) {
        LikeReply likeReply = likeReplyMapper.selectByUserIdAndReplyId(userId, replyId);
        if (likeReply == null) return null;
        return likePojoMapper.toVO(likeReply);
    }

    @Override
    public void thumbsUpReply(LikeReplyDTO likeReplyDTO) {
        // 数据转化
        LikeReply likeReply = likePojoMapper.toPO(likeReplyDTO);
        // id为空则新增点赞关联，不为空则更新点赞状态
        if (likeReply.getId() == null) {
            likeReply.setCreateTime(new Date());
            likeReply.setUpdateTime(new Date());
            int row = likeReplyMapper.insertSelective(likeReply);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        } else {
            likeReply.setUserId(null);
            likeReply.setReplyId(null);
            int row = likeReplyMapper.updateByPrimaryKeySelective(likeReply);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
        // 同步回复点赞数
        Reply reply = replyMapper.selectByPrimaryKey(likeReplyDTO.getReplyId());
        Reply updateReply = new Reply();
        updateReply.setId(likeReplyDTO.getReplyId());
        if (likeReplyDTO.getIsLiked() == 1) {
            updateReply.setLikeCount(reply.getLikeCount()+1);
            int row = replyMapper.updateByPrimaryKeySelective(updateReply);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        } else {
            updateReply.setLikeCount(reply.getLikeCount()-1);
            int row = replyMapper.updateByPrimaryKeySelective(updateReply);
            if (row != 1) throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
    }
}
